package com.sbl.bajw.pc.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.mapper.CameraHouseModelDao;
import com.sbl.bajw.mapper.CameraModelDao;
import com.sbl.bajw.pc.model.AttFileModel;
import com.sbl.bajw.pc.model.CameraHouseModel;
import com.sbl.bajw.pc.model.CameraModel;
import com.sbl.bajw.util.ExecptionUtil;
import com.sbl.bajw.util.LogUtil;

@Service
public class CameraService {
	
	@Resource
	private CameraModelDao dao;
	
	@Resource
	private CameraHouseModelDao chdao;
	
	@Resource
	private AttFileService attfileService;
	
	@Resource
	private MyBatisDao gridDao;
	
	static Logger log;
	static Logger loge;
	
	static {
		log = LogUtil.getLogger("Camera");
		loge = LogUtil.getLogger("CameraMore");
	}
	
	@Transactional(readOnly = false)
	public CameraHouseModel insert(CameraModel model) throws Exception {
		try {
			log.info(model.getCreater() + "开始入库，属性"+model.toString()+",经度："+RequestUnit.getString("lon"));
			checkModel(model);
			if( model.getLon() < 100){
				loge.info(model.getCreater() + "经纬度不合法");
				throw new MessageException("经纬度不合法", "3");
			}
			if(model.getUuid() != null && dao.getByUUID(model.getUuid()) > 0){
				loge.info(model.getCreater() + "重复提交");
				throw new MessageException("重复提交", "3");
			}
			uploadFile(model);
			dao.insert(model);
			CameraHouseModel chModel = chdao.getModel(model.getHouseAddress()); 
			log.info(model.getCreater() + "入库成功");
			return chModel;
		} catch (Exception e) {
			String msg = ExecptionUtil.getStackTrace(e);
			log.info("录入异常："+msg);
			throw e;
		}
	}
	
	public void updateApp(CameraModel model) throws Exception{
		try {
			log.info(model.getCreater() + "开始修改，属性"+model.toString());
			String userid = RequestUnit.getString("user_id");
			model.setUpdater(userid);
			model.setIsQuelity("0");
			uploadFileU(model);
			dao.update(model);
			log.info(model.getCreater() + "修改成功");
		} catch (Exception e) {
			log.info(model.toString() +"修改异常：" + e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}
	
	public void update(CameraModel model){
		dao.update(model);
	}
	
	public List<HashMap<String, String>> getCenterCount(HashMap<String, String> parm){
		List<HashMap<String, String>> result = dao.getCenterCount(parm);
		return result;
	}
	
	public List<HashMap<String, String>> getModelsForMap(HashMap<String, String> parm){
		List<HashMap<String, String>> result = dao.getModelsForMap(parm);
		return result;
	}
	
	public HashMap<String, Object> getStatisticOnDay(){
		HashMap<String, Object> map = dao.getStatisticOnDay();
		return map;
	}
	
	public String getEasyList(HashMap<String, Object> parm){
		String result = gridDao.selectPage(CameraModelDao.class, parm, "getEasyList");
		return result;
	}
	
	public String getList(HashMap<String, Object> parm){
		String result = gridDao.selectPage(CameraModelDao.class, parm, "getList");
		return result;
	}
	
	public List<CameraModel> download(HashMap<String, Object> parm){
		List<CameraModel> list = dao.getList(parm);
		return list;
	}
	
	public String uploadFile(CameraModel model) throws Exception {
		String id = dao.getCameraId();

		if (id == null || id.equals("")) {
			throw new MessageException("ID不能为空", "2");
		}

		String resultPath = "";
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssS");
		
		FileItem item = null;
		int i = 1;
		while((item = RequestUnit.getFileItem("img" + i)) != null){
			String dataStr = sf.format(Calendar.getInstance().getTime());
			
			AttFileModel filemodel = new AttFileModel();
			filemodel.setBusinessType("5");
			filemodel.setFileDir("camera");
			filemodel.setFileName(dataStr + i +  "_img_" + id);
			filemodel.setGroupid(id);
			filemodel.setItem(item);
			filemodel.setUserid(model.getCreater());
			filemodel.setFiletype("img");
			attfileService.insert(filemodel);
			log.info(model.getCreater() + "图片上传成功：" + resultPath);
			i++;
		}
		
		model.setId(id);
		
		return resultPath;
	}
	
	public String uploadFileU(CameraModel model) throws Exception {

		String resultPath = "";
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssS");
		
		FileItem item = null;
		int i = 1;
			
		while((item = RequestUnit.getFileItem("img" + i)) != null){
			String dataStr = sf.format(Calendar.getInstance().getTime());
			
			AttFileModel filemodel = new AttFileModel();
			filemodel.setBusinessType("5");
			filemodel.setFileDir("camera");
			filemodel.setFileName(dataStr + i +  "_img_" + model.getId());
			filemodel.setGroupid(model.getId());
			filemodel.setItem(item);
			filemodel.setUserid(model.getUpdater());
			filemodel.setFiletype("img");
			attfileService.insert(filemodel);
			log.info(model.getCreater() + "图片上传成功：" + resultPath);
			i++;
		}
		
		return resultPath;
	}
	
	public List<HashMap<String, String>> getPics(String id){
		List<HashMap<String, String>> pics = dao.getPics(id);
		return pics;
	}
	
	public CameraModel getModel(String id){
		CameraModel model = dao.getModel(id);
		return model;
	}
	
	public List<CameraModel> getModelsByIds(){
		String idsStr = RequestUnit.getString("ids");
		String[] ids = idsStr.split(",");
		if(ids.length < 1){
			return null;
		}
		List<CameraModel> models = dao.getModelsByIds(ids);
		return models;
	}
	
	public List<HashMap<String, Object>> getListApp(HashMap<String, Object> parm){
		List<HashMap<String, Object>> list = dao.getListApp(parm);
		return list;
	}
	
	public int getCountApp(HashMap<String, Object> parm){
		int num = dao.getCountApp(parm);
		return num;
	}
	
	public void checkModel(CameraModel model) throws MessageException{
		if(model.getHouseAddress() == null || model.getHouseAddress().equals("")){
			loge.info(model.getCreater() + "所属监控室不能为空");
			throw new MessageException("所属监控室不能为空");
		}
		
		if(model.getBuildingCode() == null || model.getBuildingCode().trim().equals("")){
			loge.info(model.getCreater() + "楼栋编码不能为空");
			throw new MessageException("楼栋编码不能为空");
		}
	}
}
